package cn.lili.xiaoran.controller;

import cn.lili.xiaoran.entity.LiIndustry;
import cn.lili.xiaoran.entity.LiQuestions;
import cn.lili.xiaoran.entity.dtos.LiIndustryDTO;
import cn.lili.xiaoran.mapper.LiQuestionsMapper;
import cn.lili.xiaoran.service.LiIndustryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import cn.lili.mybatis.util.PageUtil;
import cn.lili.common.enums.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.SearchVO;
import cn.lili.common.vo.ResultMessage;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * 行业(LiIndustry)表控制层
 *
 * @author makejava
 * @since 2022-08-09 16:23:19
 */
@RestController
@Api(tags = "liIndustry接口")
@RequestMapping("/manager/lili/liIndustry")
@Transactional(rollbackFor = Exception.class)
public class LiIndustryController {
    /**
     * 服务对象
     */
    @Resource
    private LiIndustryService liIndustryService;

    @Resource
    private LiQuestionsMapper liQuestionsMapper;

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "查看${tableInfo.description}详情")
    public ResultMessage<LiIndustry> get(@PathVariable Integer id) {

        LiIndustry liIndustry = liIndustryService.getById(id);
        return new ResultUtil<LiIndustry>().setData(liIndustry);
    }

    @GetMapping
    @ApiOperation(value = "分页获取${tableInfo.description}")
    public ResultMessage<IPage<LiIndustry>> getByPage(LiIndustryDTO page) {
        IPage<LiIndustry> data = this.liIndustryService.page(PageUtil.initPage(page), page.queryWrapper());
        return new ResultUtil<IPage<LiIndustry>>().setData(data);
    }

    @PostMapping
    @ApiOperation(value = "新增${tableInfo.description}")
    public ResultMessage<LiIndustry> save( LiIndustry liIndustry) {

        if (this.liIndustryService.save(liIndustry)) {
            return new ResultUtil<LiIndustry>().setData(liIndustry);
        }
        return new ResultUtil<LiIndustry>().setErrorMsg("未知异常，请稍后重试");
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新${entity.description}")
    public ResultMessage<LiIndustry> update(@PathVariable String id,  LiIndustry liIndustry) {
        if (this.liIndustryService.updateById(liIndustry)) {
            return new ResultUtil<LiIndustry>().setData(liIndustry);
        }
        return new ResultUtil<LiIndustry>().setErrorMsg("未知异常，请稍后重试");
    }

    @DeleteMapping(value = "/{ids}")
    @ApiOperation(value = "删除行业")
    public ResultMessage<Object> delAllByIds(@PathVariable List ids) {

        liIndustryService.removeByIds(ids);
        return ResultUtil.success("成功删除");
    }


    @GetMapping(value = "/allcount")
    @ApiOperation(value = "查看${tableInfo.description}详情")
    public ResultMessage<Map<String,Object>> allcount() {
        Map<String,Object> result=new HashMap<>();
        QueryWrapper<LiQuestions> tiwenshu=new QueryWrapper<>();
        tiwenshu.eq("delete_flag",0);
        tiwenshu.eq("isweidu",1);
        Long count=liQuestionsMapper.selectCount(tiwenshu);
        result.put("xinttiwen",count);
        ResultMessage res=new ResultMessage();
        res.setResult(result);
        return res;
    }

}

